package edu.washington.cs.workflow.analysis;

import java.util.Collection;

import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.cha.ClassHierarchy;

import edu.washington.cs.workflow.util.Utils;
import edu.washington.cs.workflow.util.WALAUtils;

/**
 * The code for comparison experiment, find all reachable methods for an event handler
 * */
public class EventReachableMethodFinder {

	public final AnalysisBuilder builder;
	private CallGraph cg = null;
	public final String[] appPackages;
	public final ClassHierarchy cha;
	
	public EventReachableMethodFinder(AnalysisBuilder builder) {
		Utils.checkNotNull(builder);
		this.builder = builder;
		this.cg = builder.getCallGraph();
		this.appPackages = builder.appPackages;
		this.cha = builder.getClassHierarchy();
	}
	
	private Collection<CGNode> cachedHandlers = null;
	public Collection<CGNode> getAllEventHandlers() {
		if(cachedHandlers == null) {
			cachedHandlers = SwingHelper.getEventHandlingMethods(cg, cha, appPackages);
		}
		return cachedHandlers;
	}
	public CGNode getEventHandler(String signature) {
		if(cachedHandlers == null) {
			cachedHandlers = SwingHelper.getEventHandlingMethods(cg, cha, appPackages);
		}
		CGNode handler = WALAUtils.findCGNode(cachedHandlers, signature);
		return handler;
	}
	
	public Collection<CGNode> getReachableNode(CGNode node) {
		return WALAUtils.getReachableAppNodes(node, cg, appPackages);
	}
}